<template>
  <!-- 树状列表组件 -->
  <div style="width: 100%; height: 100%; overflow: auto">
    <el-tree
      ref="catetreeone"
      :data="treedata"
      :expand-on-click-node="false"
      :node-key="'facilityNo'"
      :default-expanded-keys="treeList"
      accordion
      :highlight-current="true"
      :props="treePropstype"
      @node-click="handleSelectionChange"
    />
  </div>
  <!-- show-checkbox default-expand-all-->
</template>
<script>
// import nowpagereq from "@/api/basic-info-manage/camera-management/index";
import nowpagereq from "@/api/basic-info-manage/fence-management/index";

export default {
  props: {
    facilityType: {
      type: String,
      default: () => ""
    }
  },
  data() {
    return {
      treePropstype: {
        children: "facilityList",
        label: "facilityName"
      },
      treedata: [],
      facilityGroupList: [],
      facilityList: [],
      treeList: []
    };
  },
  // watch: {
  //   facilityType() {
  //     this.gettreelist();
  //   }
  // },
  async mounted() {
    await this.gettreelist();
    // setTimeout(() => {
    this.$nextTick(() => {
      if (this.treedata.length > 0) {
        // if(this.treedata[0].)
        // this.handleSelectionChange(this.treedata[0].facilityList[0]);
        this.handleSelectionChange(this.treedata[0]);
        // console.log(this.treedata[0].facilityNo)
        this.$nextTick(() => {
          this.$refs.catetreeone.setCurrentKey(this.treedata[0].facilityNo);
        });
      } else {
        this.handleSelectionChange(this.treedata[0]);
      }
    });
    // console.log(this.treedata[0])

    // }, 1000);
  },
  methods: {
    handleSelectionChange(e) {
      // console.log((e))
      this.$emit("treeselectvalue", e);
    },
    // 获取树状数据
    async gettreelist() {
      const res = await nowpagereq.conditionQuery({
        // facilityType: this.facilityType || "羊舍",
        typeList: ["羊舍", "虚拟舍"],
        status: 0
      });
      let data = res.data;
      this.facilityGroupList = data;
      // this.facilityList = data.facilityList;
      if (this.facilityGroupList.length > 0) {
        this.treeList = [this.facilityGroupList[0].facilityNo];
      } else {
        this.treeList = [this.facilityList[0].facilityNo];
      }
      this.changetreedata();
    },
    // 处理树状数据
    changetreedata() {
      let nothasgroup = [];
      if (this.facilityList.length > 0) {
        // nothasgroup = [
        //   {
        //     facilityName: "未分组地块",
        //     facilityNo: this.facilityList[0].facilityNo,
        //     facilityList: this.facilityList
        //   }
        // ];
      } else {
        // nothasgroup = [
        //   {
        //     facilityName: "未分组地块",
        //     facilityList: this.facilityList
        //   }
        // ];
      }
      let hasgrouplist = this.facilityGroupList.filter(item => {
        // item.facilityName = item.facilityName
        // item.facilityNo = item.facilityNo
        this.$set(item, "facilityName", item.facilityName);
        this.$set(item, "facilityNo", item.facilityNo);
        // console.log(item)
        let groupitem = {
          ...item
        };
        return groupitem;
      });
      this.treedata = [...hasgrouplist, ...nothasgroup];
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep {.el-tree {
  overflow-y: unset;
  padding-right: 8px;
  .el-tree-node {
    border: #59A1E8 1px solid;
    & {
      border-bottom: #59A1E8 0rem solid;
    }
    &:nth-last-child(2) {
      border-bottom: #59A1E8 1px solid;
    }
    &.is-current .el-tree-node__label, &:hover .el-tree-node__label {
      color: #fff !important;

    }
  }
  .el-tree-node__expand-icon {
    display: none;
  }
  .el-tree-node__label {
    text-align: center;
    color: #59A1E8 !important;
    font-size: 16px;
font-weight: 400;
letter-spacing: 0px;
line-height: 23.17px;
text-align: center;
vertical-align: top;
  }
  .el-tree-node,.el-tree-node__label,.el-tree-node__content {
    background-color:transparent ;
  }}}
</style>
